<?php

namespace App\Http\Controllers\Api\V3;

use App\Http\Controllers\Api\V2\BaseController;
use App\Models\V1\MemberModel;
use App\Models\V1\ReadPartyModel;
use Illuminate\Http\Request;
use App\Models\V1\CompanyModel;
use App\Common\ApiData;

class CompanyController extends BaseController
{

    /**
     * 添加部门
     * */
    public function add_department(Request $request)
    {

        //1.检查参数
        $mobile         = $request->input('mobile');
        $key            = $request->input('key');
        $companyId      = $request->input('companyId');
        $departmentName = $request->input('departmentName');
        $apiData        = new ApiData();


        $params      = [
            'mobile'    => [$mobile, "缺少手机号", "3001"],
            'key'       => [$key, "缺少设备ID", "3002"],
            'companyId' => [$companyId, "缺少公司ID", '3003'],
            'deName'    => [$departmentName, "缺少部门名称", "3004"]
        ];
        $checkResult = $this->check_params($params);
        if ($checkResult->status == false) {
            $apiData->message = $checkResult->message;
            $apiData->code    = $checkResult->code;
            return $apiData->send();
        }

        $memberModel = new MemberModel();
        $memberInfo  = $memberModel->get_member_info_by_mobile_key($mobile, $key);
        if ($memberInfo == false) {
            $apiData->message = "用户不存在";
            $apiData->code    = "2001";
            return $apiData->send();
        }

        $data         = ['company_id' => $companyId, 'department_name' => $departmentName];
        $companyModel = new CompanyModel();
        $res          = $companyModel->update_department($data);
        if ($res) {
            $apiData->message = "添加成功";
            $apiData->code    = "200";
        } else {
            $apiData->message = "添加失败";
            $apiData->code    = "402";
        }
        return $apiData->send();
    }


    /*
     * 获得部门列表
     * */
    public function get_department(Request $request)
    {
        $mobile    = $request->input('mobile');
        $key       = $request->input('key');
        $companyId = $request->input('companyId');
        $apiData   = new ApiData();

        $companyModel = new CompanyModel();
        $departments  = $companyModel->get_company_department($companyId);

        $apiData->message = "获取成功";
        $apiData->code    = "200";
        $apiData->set_data('departments', $departments);
        return $apiData->send();
    }


    /**
     * 获得公司架构信息
     * */
    public function get_company_orgization(Request $request)
    {
        $mobile      = $request->input('mobile');
        $key         = $request->input('key');
        $readPartyId = $request->input('readPartyId');
        $apiData     = new ApiData();

        $memberModel = new MemberModel();
        $memberInfo  = $memberModel->get_member_info_by_mobile_key($mobile, $key);
        if ($memberInfo == false) {
            $apiData->message = "用户不存在";
            $apiData->code    = "2001";
            return $apiData->send();
        }

        $readPartyModel = new ReadPartyModel();
        $readPartyInfo  = $readPartyModel->get_read_party_info($readPartyId);
        if ($readPartyInfo == false) {
            $apiData->message = "读书会不存在";
            $apiData->code    = "3000";
            return $apiData->send();
        }

        $companyId    = $readPartyInfo->company_id;
        $companyModel = new CompanyModel();
        $companyInfo  = $companyModel->get_company_info($companyId);

        if (!$companyInfo) {
            $apiData->message = "读书会不存在";
            $apiData->code    = "3001";
            return $apiData->send();
        }
        $departments = $companyModel->get_company_department($companyId);
        foreach ($departments as $key => $depart) {
            $members             = $readPartyModel->get_read_party_department_member($readPartyId, $depart->department_id);
            $depart->members     = $members;
            $depart->totalMember = count($members);
        }
        $otherMembers = $readPartyModel->get_read_party_department_member($readPartyId, 0);
        $otherDepartment                  = new \stdClass();
        $otherDepartment->department_id   = 0;
        $otherDepartment->department_name = "未分组";
        $otherDepartment->company_id      = $companyId;
        $otherDepartment->totalMember     = count($otherMembers);
        $otherDepartment->members         = $otherMembers;
        $departments                      = $departments->toArray();
        array_push($departments, $otherDepartment);

        $apiData->message = "ok";
        $apiData->code    = "200";
        return $apiData->set_data('companyInfo', $companyInfo)
            ->set_data('departments', $departments)
            ->send();
    }


}